package com.yazi.mxz.admin.controller;

import java.util.List;

import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.jackson.result.ListResult;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.Region;
import com.yazi.mxz.common.service.RegionService;
import com.yazi.mxz.admin.jackson.filter.RegionFilter;

@Controller("regionController")
@RequestMapping("/region")
public class RegionController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(RegionController.class);

	@Resource
	RegionService regionService;
	
	@RequestMapping("/list")
	public String list(){
		log.debug("进入地区列表页面");
		return "region";
	}
	
	
	@RequestMapping("/listByParentId/json")
	@ResponseBody
	public ListResult<?> listByParentIdJson(Long parentId) {
		log.debug("管理  -> 地区json数据");
		ListResult<Region> jsonResult = new ListResult<Region>();
		jsonResult.setFilter(RegionFilter.get());
		try{
			Region region = new Region();
			region.setParentId(parentId);
			List<Region> regions = regionService.list(region);
			
			jsonResult.setResult(regions);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("管理 - > 获取地区列表异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
	@RequestMapping("/listBrothers/json")
	@ResponseBody
	public ListResult<?> listBrothersJson(Long regionId) {
		log.debug("地区json数据");
		ListResult<Region> jsonResult = new ListResult<Region>();
		jsonResult.setFilter(RegionFilter.get());
		try{
			Region region = regionService.one(regionId);
			if (region.getType() > 0) {
				Region regionForSearch = new Region();
				regionForSearch.setParentId(region.getParentId());
				List<Region> regions = regionService.list(regionForSearch);
				jsonResult.setResult(regions);
			}
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("管理 - > 获取兄弟地区列表异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
	@RequestMapping("/addDo/json")
	@ResponseBody
	public ObjectResult<?> addDo(Long parentId, Integer type){
		log.debug("新增地区");
		ObjectResult<Region> jsonResult = new ObjectResult<Region>();
		jsonResult.setFilter(RegionFilter.get());
		try{
			Region region = new Region();
			region.setParentId(parentId);
			region.setName("新增地区");
			region.setType(type + 1);
			regionService.add(region);
			
			jsonResult.setResult(region);
			jsonResult.setOk();
		} catch (MyException e) {
			log.debug("新增地区失败: {}", e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			log.error("新增地区异常", e);
			jsonResult.setErrorMessage("操作失败");
		}
		return jsonResult;
	}
	
	@RequestMapping("/editDo/json")
	@ResponseBody
	public StatusResult editDo(Region region){
		log.debug("修改地区");
		StatusResult jsonResult = new StatusResult();
		try{
			regionService.edit(region);
			
			jsonResult.setOk();	
		} catch (MyException e) {
			log.debug("修改地区失败: {}", e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			log.error("修改地区异常", e);
			jsonResult.setErrorMessage("更新失败");
		}
		return jsonResult;
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDo(Long regionId){
		log.debug("删除地区");
		StatusResult jsonResult = new StatusResult();
		try{
			regionService.delete(regionId);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("删除地区异常", e);
			jsonResult.setErrorMessage("删除失败");
		}
		return jsonResult;
	}
	
}
